/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package payment.paypal;

import com.paypal.sdk.core.nvp.NVPDecoder;
import com.paypal.sdk.core.nvp.NVPEncoder;
import com.paypal.sdk.profiles.APIProfile;
import com.paypal.sdk.profiles.ProfileFactory;
import com.paypal.sdk.services.NVPCallerServices;
import java.util.Map;

/**
 *
 * @author WangShuai
 *
 */
public class Demo1 {

    /**
     * NVP: Name-Value Pair  NVP是指定字符串中的名称和值的一种方法。NVP是表示URI规范中的查询的非正式名称。NVP字符串附加到URL上。
     *
     */
    private NVPCallerServices caller = null;

    public String doDirectPaymentCode(String paymentAction, String amount, String cardType,
            String acct, String expdate, String cvv2, String firstName,
            String lastName, String street, String city, String state,
            String zip, String countryCode) {
        NVPEncoder encoder = new NVPEncoder();
        NVPDecoder decoder = new NVPDecoder();

        try {

            caller = new NVPCallerServices();
            APIProfile profile = ProfileFactory.createSignatureAPIProfile();
            // Set up your API credentials, PayPal end point, API operation and version.
            profile.setAPIUsername("tong_api1.china-enterprise-forum.com");
            profile.setAPIPassword("PT9MGDA4ESNSUUJ7");
            profile.setSignature("AxjF1y7Tr1g4r4V5PDoN13EqGqfyAbDf9Vq8V2S5hpjhjvA8402gGeWB");
            profile.setEnvironment("live");
            profile.setSubject("");
            caller.setAPIProfile(profile);

            encoder.add("METHOD", "DoDirectPayment");
            //encoder.add("IPADDRESS", "192.168.0.1");
            // Add request-specific fields to the request string.
            encoder.add("PAYMENTACTION", paymentAction);
            encoder.add("AMT", amount); //The total cost of the transaction to the customer
            encoder.add("ACCT", acct);  //Credit card number.
            encoder.add("EXPDATE", expdate); //Credit card expiration date.  Format: MMYYYY
            encoder.add("CVV2", cvv2); //Card Verification Value, version 2.  Your Merchant Account settings determine whether this field is required.
            
            //            encoder.add("CREDITCARDTYPE", cardType);  //Credit type:  MasterCard  Visa
            //            encoder.add("FIRSTNAME", firstName); //Payer’s first name.
            //            encoder.add("LASTNAME", lastName);  //Payer’s last name.
            //            encoder.add("STREET", street);//First street address.
            //            encoder.add("CITY", city);
            //            encoder.add("STATE", state);
            //            encoder.add("ZIP", zip);//U.S. ZIP code or other country-specific postal code.
            //            encoder.add("COUNTRYCODE", countryCode); //Country code.

            // Execute the API operation and obtain the response.
            String NVPRequest = encoder.encode();
            String NVPResponse = (String) caller.call(NVPRequest);
            decoder.decode(NVPResponse);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Map map = decoder.getMap();
        for (Object o : map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            System.err.println(entry.getKey() + " : " + entry.getValue());
        }
        return decoder.get("ACK");
    }

    public String setExpressCheckoutCode(String returnURL, String cancelURL, String amount,
            String paymentType, String currencyCode) {

        NVPEncoder encoder = new NVPEncoder();
        NVPDecoder decoder = new NVPDecoder();

        try {
            caller = new NVPCallerServices();
            APIProfile profile = ProfileFactory.createSignatureAPIProfile();
            // Set up your API credentials, PayPal end point, API operation and version.
            //            profile.setAPIUsername("sdk-three_api1.sdk.com");
            //            profile.setAPIPassword("QFZCWN5HZM8VBG7Q");
            //            profile.setSignature("AVGidzoSQiGWu.lGj3z15HLczXaaAcK6imHawrjefqgclVwBe8imgCHZ");
            profile.setAPIUsername("tong_api1.china-enterprise-forum.com");
            profile.setAPIPassword("PT9MGDA4ESNSUUJ7");
            profile.setSignature("AxjF1y7Tr1g4r4V5PDoN13EqGqfyAbDf9Vq8V2S5hpjhjvA8402gGeWB");
            profile.setEnvironment("live");
            profile.setSubject("");
            caller.setAPIProfile(profile);
            //encoder.add("VERSION", "51.0");
            encoder.add("METHOD", "SetExpressCheckout");
            // Add request-specific fields to the request string.
            //The return URL is the page to which PayPal redirects your buyer’s browser after the buyer logs into PayPal and approves the payment.
            //用户登陆PayPal且同意支付后跳转页面
            encoder.add("RETURNURL", returnURL);
            //The cancel URL is the page to which PayPal redirects your buyer’s browser if the buyer does not approve the payment.
            encoder.add("CANCELURL", cancelURL);
            //The format must have decimal point with exactly two digits to the right and an optional thousands separator to the left, which must be a comma.
            //For example, EUR 2.000,00 must be specified as 2000.00 or 2,000.00. The specified amount cannot exceed USD $10,000.00, regardless of the currency used.
            //【数据格式要正确，不能超过1万美元】
            encoder.add("PAYMENTREQUEST_0_AMT", amount);//The total cost of the transaction to the customer
            encoder.add("MAXAMT", amount);
            //encoder.add("PAYMENTREQUEST_0_CURRENCYCODE", PayPalConfig.CURRENCY_CODE);

            // Execute the API operation and obtain the response.
            String NVPRequest = encoder.encode();
            String NVPResponse = caller.call(NVPRequest);
            decoder.decode(NVPResponse);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        Map map = decoder.getMap();
        for (Object o : map.entrySet()) {
            Map.Entry entry = (Map.Entry) o;
            System.err.println(entry.getKey() + " : " + entry.getValue());
        }
        return decoder.get("ACK");
    }

    public static void main(String[] args) {
        Demo1 directPay = new Demo1();
        String paymentAction = "Sale";
        String amount = "0.01";
        String cardType = "Visa";
        String acct = "4520840001578824";
        String expdate = "082011";
        String cvv2 = "222";
        String firstName = "Testadse";
        String lastName = "User";
        String street = "1 Maire-Victorin";
        String city = "Toronto";
        String state = "ON";
        String zip = "M5A 1E1";
        String countryCode = "CA";
        //directPay.doDirectPaymentCode(paymentAction, amount, cardType, acct, expdate, cvv2, firstName, lastName, street, city, state, zip, countryCode);
        //directPay.setExpressCheckoutCode("http://www.hengzhi.cc", "http://www.hengzhi.cc", amount, cardType, countryCode);
    }
}
